Skip to content

Warn when a renamed record would replace another record's signal files - #675

Merged
tompollard merged 2 commits into
mainfrom
warn-overwrite-other-record-files
Nov 24, 2026
Merged

tompollard merged 2 commits into
mainfrom
warn-overwrite-other-record-files

Conversation

@tompollard

@tompollard tompollard commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Fixes #153.

Record.wrsamp names the header after record_name, but the signal files by file_name. So the example in the issue, reading record 100, setting record_name = "100_cloned" and writing it, gives 100_cloned.hea and 100.dat. Next to the original record, that silently replaces the original's 100.dat. This is by design, since a record's signals may be split across several files or shared with other records. This PR makes it visible rather than changing it.

  • A warning in the risky case: wrsamp warns, before writing anything, when it would replace an existing signal file that the record's own header in write_dir doesn't list. The warning names the files, and points at the user's own wrsamp() call:

    UserWarning: Writing record '100_cloned' replaces existing signal files 100.dat, which are named by `file_name`, not `record_name`. To write a copy of a record under a new name, set `file_name` too.
    

    It isn't given in these cases:

    • rewriting a record's own files (its header lists them);
    • writing a renamed copy to another folder, or into a .wfdb archive;
    • rewriting files whose names don't match the record name, such as rec_0001e.dat for record rec_0001, as in MIMIC-IV-WDB.

    It is given for a file named like the record but belonging to another one, such as 100_extra.dat when record 100_extra is renamed to 100.

    It's a warning, not an error, so code that does this on purpose keeps working.

  • Documentation: the wrsamp docstring explains how files are named, with an example of saving a copy under a new name. The file_name description said only "The name of the file used for analysis", and now explains the same.

Naming new files after the record by default could be part of the API redesign in #672 (for example a copy(record_name=...) method), rather than a change to wrsamp.

Testing

  • New tests: the case from the issue, a renamed record whose name is a prefix of another's, and a record whose own header lists other files all warn; the four cases above don't. The first fails on main.
  • pytest -m "not network", run serially with this warning turned into an error, passes. Two existing tests wrote a record that shares its signal file with another sample record into the directory their class shares, so they replaced another test's file; they now use their own directories. Network tests, ruff and the docs build pass.

@tompollard 🟥 red 🦥 sloth · aspen

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 618775f. I found two issues:

  1. The warning misses a rename that overwrites another record (src/wfdb/io/record.py, lines 1003-1010). The file_name.startswith(record_name) check treats 100_extra.dat as belonging to record 100. I wrote a 100_extra record, changed only its record_name to 100, and wrote it again: 100_extra.dat was overwritten without a warning. This is the data-loss case the PR aims to expose. Please cover this prefix case.

  2. Existing tests fail when warnings are errors. The 116 offline record tests pass normally but emit two new UserWarnings. A focused run with -W error::UserWarning fails in test_4a, contrary to the PR testing claim. If these overwrites are intentional in the tests, please make the tests expect the warnings.

All CI checks pass. I would address these before merging.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, both fixed in 70aa455.

  1. The prefix case: you're right that the name check can't tell 100_extra.dat from a file of record 100. A file now counts as the record's own only if the record's existing header in write_dir lists it, which is the case when a record is written again. Otherwise wrsamp warns. This covers your case (renaming 100_extra to 100) and the one from wrsamp wrong output *.dat filename #153. It also covers a record whose existing header lists other files. It still doesn't warn when a record is rewritten, or for suffixed names such as rec_0001e.dat, because the header lists them. New tests cover the prefix case and the case where the record's own header lists other files. The prefix test fails on the previous commit.
  2. Existing tests: you're right, and my claim in the description was wrong. I had run the suite with the warning as an error under -n auto, and the warning filter didn't take effect in the xdist workers. Run serially, test_wrsamp_expanded_physical_only_record and test_4a both warned. They write test01_00s_frame and test01_00s_skewframe, whose signal file is test01_00s.dat, the same as the sample record test01_00s. They write into the directory that the whole class shares, so they replaced a file that another test had written. That's what the warning is for, and it depended on test order. So instead of expecting the warning, those two tests now use their own tmp_path.

pytest -m "not network" -W "error:Writing record:UserWarning", run serially, passes, as does your -W error::UserWarning run of tests/test_record.py. The network tests, ruff and the docs build pass. I've corrected the PR description.

Record.wrsamp names the header after record_name but the signal files by
file_name, so a record read and given a new record_name, then written next
to the original, silently replaced the original's signal files. Warn when
writing would replace an existing signal file whose name does not start
with record_name, and explain this, with an example, in the documentation
of wrsamp and file_name, which said only "the name of the file used for
analysis".

Fixes #153.
The warning treated any signal file whose name started with the record
name as the record's own, so renaming record 100_extra to 100 replaced
100_extra.dat without a warning. A file is now the record's own only if the
record's existing header in write_dir lists it, as when a record is written
again.

Two tests in TestRecord wrote records whose signal file is shared with
another sample record (test01_00s.dat) into the directory shared by the
class, so they replaced a file written by another test and triggered the
warning. Give them their own directories.
@tompollard
tompollard force-pushed the warn-overwrite-other-record-files branch from 70aa455 to a989032 Compare November 24, 2026 02:33
@tompollard
tompollard merged commit 05db3a2 into main Nov 24, 2026
18 checks passed
@tompollard
tompollard deleted the warn-overwrite-other-record-files branch November 24, 2026 02:37
tompollard added a commit that referenced this pull request Nov 24, 2026
## Summary

Bump the version from 5.1.0 to 5.2.0, and collect the 19 entries in
`changelog.d/` into `docs/changes.rst` with `towncrier build --date "Nov
2026"`, with a short summary at the top of the new section.

This is a minor release. The one change that existing code may notice is
that `rdann` and `Annotation.apply_range` now stop before `sampto`, as
`rdrecord` and `rdsamp` do, so an annotation exactly at `sampto` is no
longer included (#667). The summary calls it out, with how to get the
old result.

The release also includes:
- **Fixes for PhysioNet:** `get_dbs`, which PhysioNet's API change had
broken (#665), record names with folders in `pn_dir` (#666), and
`physionet://` and other URIs in `read_edf`, `rdedfann` and `read_wav`
(#674).
- **Robustness:**
  - headers whose base time or date is in another form (#668);
- multi-segment writes that leave no partial record after an error
(#673);
- a warning before `wrsamp` replaces another record's signal files
(#675).
- **New:** a beat-by-beat comparison of annotation files, as WFDB `bxb`
does, as a development feature (#679), and axis labels in `plot_wfdb`
(#670).
- **Smaller fixes:**
- integer overflow and precision in `find_peaks` and `normalize_bound`;
  - constant signals in `normalize_bound`;
  - multi-character custom annotation symbols;
  - format 61 signals read with `smooth_frames=False`;
  - skew written twice when rewriting records;
  - docstring examples, and more.

This will be the first release published by
`.github/workflows/publish.yml`, from the GitHub release, with
provenance.

## Checks

- `pytest -m "not network"`: 679 passed (4 skipped, the `bxb` comparison
that needs WFDB installed), also with `--python 3.11 --resolution
lowest-direct`. `pytest -m network`: 20 passed.
- Ruff and the Sphinx HTML build with warnings as errors pass.
- The wheel and source distribution build and pass `twine check`. They
have `Version: 5.2.0` and include `wfdb/processing/beat_comparison.py`.
- PyPI has no 5.2.0 release yet.

<sub>@tompollard 🟥 red 🦥 sloth · aspen</sub>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wrsamp wrong output *.dat filename

1 participant